package com.acce.product.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.acce.product.jpush.PushMessage;
import com.alibaba.fastjson.JSONObject;

public class PushMsgUtil {

	private static Logger logger = Logger.getLogger(PushMsgUtil.class);

	public static final String PUSH_URL = "http://api.jpush.cn:8800/sendmsg/sendmsg";

	private static final String appKey = PropertiesUtil.getInstance().getProperties("jpush/jpush_cfg.properties").get("appKey").toString();

	private static final String masterSecret = PropertiesUtil.getInstance().getProperties("jpush/jpush_cfg.properties").get("masterSecret").toString();

	private static JPushClient jpush = null;

	public static void pushAndroidNotificationMessage(PushMessage pm) {

		jpush = new JPushClient(masterSecret, appKey);
		// 在实际业务中，建议 sendNo 是一个你自己的业务可以处理的一个自增数字。
		// 除非需要覆盖，请确保不要重复使用。详情请参考 API 文档相关说明。
		logger.info("jpush::push message-" + pm.getMsgContent());
		PushPayload payload = PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setNotification(Notification.newBuilder()
						.setAlert(pm.getAlert())
						.addPlatformNotification(AndroidNotification.newBuilder()
        				.setTitle(pm.getTitle()).build()).build())
				.setAudience(
					Audience.newBuilder()
						.addAudienceTarget(
							AudienceTarget.tag(pm.getTag()))
						.addAudienceTarget(
							AudienceTarget.alias(pm.getAlias()))
						.build())
				.setMessage(
						Message.newBuilder().setMsgContent(pm.getMsgContent())
						.setTitle(pm.getTitle())
//						.setContentType(pm.getContentType())
						.addExtra("from", "农商易-商品管理服务器").build()).build();
		// 对所有用户发送通知, 更多方法请参考文档
		logger.info("jpush::payload-" + payload.toString());
        PushResult msgResult = null;
		try {
			msgResult = jpush.sendPush(payload);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (null != msgResult) {
			logger.info("server returned: " + msgResult.toString());
		} else {
			logger.info("no data!");
		}

	}

	public static void main(String[] args) throws Exception {
		String msg = "{\"n_title\":\"来点外卖\",\"n_content\":\"你好\"}";
		System.out.println(msg);
//		PushMsgUtil.pushMessage(msg);
		// PushMsgUtil.pushMsg(msg);
		String apiUrl = "https://api.jpush.cn/v3/push";
		JSONObject json = new JSONObject();
		json.put("platform", "all");
		json.put("audience", "all");

		JSONObject android = new JSONObject();
		android.put("alert", "Hello, JPush!");
		android.put("title", "JPush test");
		android.put("builder_id", 3);
		JSONObject extra = new JSONObject();
		extra.put("param1", "fuck you!");
		android.put("extras", extra);
		
		json.put("notification", android);
		
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "Basic " + AlgorithmsUtil.encodeBase64((appKey+":"+masterSecret).getBytes()));
		System.out.println(HttpClientUtil.doPostSSL(apiUrl, json, headers));
	}

}
